package com.jogern.baselibrary;

import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.MotionEvent;

/**
 * 最好使用在Fragrant是共用同一个容器
 * Created by jogern on 2016/8/15 0015.
 */

public abstract class BaseFragmentActivity extends BaseActivity {
    private int contentId;

    public void setContentId(int contentId) {
        this.contentId = contentId;
    }

    protected void addFragment(Fragment fragment) {
        addFragmentInStack(contentId, fragment);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Fragment popFragment = getPopFragmentOfStack();
        if (popFragment != null && popFragment instanceof BaseFragment) {
            if (((BaseFragment) popFragment).onTouchEvent(event)) {
                return true;
            }
        }
        return super.onTouchEvent(event);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Fragment popFragment = getPopFragmentOfStack();
        if (popFragment != null && popFragment instanceof BaseFragment) {
            if (((BaseFragment) popFragment).onKeyDown(keyCode,event)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onBackPressed() {
        int entryCount = getSupportFragmentManager().getBackStackEntryCount();
        if (entryCount > 1) {
            super.onBackPressed();
        } else {
            finish();
        }
    }
}
